﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static LiWi.Shell;

namespace LiWiTools
{
    public partial class ConnectForm : Form
    {
        public ConnectForm()
        {
            InitializeComponent();
            IniData();
        }

        public string newConnect;
        /// <summary>
        /// 窗口初始化参数
        /// </summary>
        void IniData()
        {
            txt_comid.Text = "1";
            txt_rate.Text = "115200";
            com_parity.Items.Add("无校验");
            com_parity.Items.Add("奇校验");
            com_parity.Items.Add("偶校验");
            com_parity.Text = "无校验";
            com_parity.DropDownStyle = ComboBoxStyle.DropDownList;
            com_ip.Items.Add("127.0.0.1");
            com_ip.Items.Add("192.168.1.11");
            com_ip.Text = "192.168.1.11";
            com_controlMode.Items.Add("ModBus_TCP");
            com_controlMode.Items.Add("ModBus_RTU");
            com_controlMode.Text = "ModBus_TCP";
            com_controlMode.DropDownStyle = ComboBoxStyle.DropDownList;

            Btn_COMConnect.Click += (sender, e) => ComCon();
            Btn_IPConnect.Click += (sender, e) => IPCom();
        }

        /// <summary>
        /// Com连接
        /// </summary>
        void ComCon()
        {
            newConnect = null;
            uint comid = Convert.ToUInt32(txt_comid.Text);  //串口号
            uint rate=Convert.ToUInt32(txt_rate.Text);      //波特率
            uint parity=Convert.ToUInt32(com_parity.SelectedIndex);  //检验位
            PMC_SetComDefaultBaud(115200, 8, 0, 0);
            string COMName = $"COM{comid}";
            foreach (var item in ToolsForm.controlDictionary)
            {
                if (COMName == item.Key)
                {
                    MessageBox.Show($"{COMName}:当前串口已有连接");
                    return;
                }
            }
            lock (ToolsForm.controlDictionaryLock)
            {
                ToolsForm.controlDictionary[COMName] = new Data();
            }
            int ret = PMC_OpenCom(comid, ref ToolsForm.controlDictionary[COMName].handle);
            if ( ret != 0)
            {
                MessageBox.Show("连接失败，请使用其他连接方式！！");
                return;
            }
            ToolsForm.controlDictionary[COMName].connectID = "COM" + comid;
            ToolsForm.controlDictionary[COMName].connectOver = true;
            newConnect= "COM" + comid; 
            this.Close();
        }

        /// <summary>
        /// IP连接
        /// </summary>
        void IPCom()
        {
            newConnect= null;
            string ip=com_ip.Text;
            foreach (var item in ToolsForm.controlDictionary)
            {
                if (ip==item.Key)
                {
                    MessageBox.Show($"{ip}:当前IP已有连接");
                    return;
                }
            }
            lock (ToolsForm.controlDictionaryLock)
            {
                ToolsForm.controlDictionary[ip] = new Data();
            }
            int ret;
            if(com_controlMode.SelectedIndex==0)    ret = PMC_OpenEth(ip, ref ToolsForm.controlDictionary[ip].handle);
            else ret=PMC_OpenEthModbusRTU(ip, ref ToolsForm.controlDictionary[ip].handle);
            if (ret != 0)
            {
                MessageBox.Show("自动连接失败，请使用其他连接方式！！");
                return;
            }
            
            ToolsForm.controlDictionary[ip].connectID = ip;
            ToolsForm.controlDictionary[ip].connectOver = true;
            newConnect = ip;
            this.Close();
        }


    }
}
